<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Config;
use app\common\model\DiamondLog;
use app\common\model\GoldLog;
use app\common\model\MoneyLog;
use app\common\model\prize\Lists;
use app\common\model\score\Orders;
use app\common\model\ScoreLog;
use think\Db;
use think\Exception;

class Prize extends Api
{
    protected $noNeedLogin = "";
    protected $noNeedRight = "*";

    public function lists()
    {
        $this->success("", Lists::field("id,name,logo")->select());
    }

    //抽奖
    public function lotterys()
    {
        $lottery_score = Config::where('name', 'lottery_score')->value('value', 1);
        if ($lottery_score > $this->auth->score) {
            $this->error("当前积分不足");
        }

        $lists = Db::name("prize_lists")->where("probability", ">", 0)->orderRaw("rand()")->select();
        $sum = Db::name("prize_lists")->sum("probability");

        $result = rand(0, $sum * 100);
        $begin = 0;
        $id = 0;
        $stock = 0;
        $data = [];
        foreach ($lists as $value) {
            $probability = $value['probability'] * 100;
            if ($begin < $result && $result < $probability) {
                $id = $value['id'];
                $stock = $value['stock'];
                $data = $value;
            }
            $begin = $probability;
        }

        //兼容无
        if ($id == 0 || $stock == 0) {
            $data = [
                'id' => 0,
                'name' => '祝你好运',
                'logo' => $this->request->domain() . 'assets/img/good/png',
                'award_type' => 5
            ];
        }

        Db::startTrans();
        try {
            $user = \app\common\model\User::get($this->auth->id);
            $before = $user->score;
            $after = function_exists('bcadd') ? bcadd($user->score, -$lottery_score, 2) : $user->score - $lottery_score;
            ScoreLog::create([
                'user_id' => $user->id, 'score' => $lottery_score,
                'before' => $before, 'after' => $after,
                'memo' => "抽奖消耗", 'type' => 3,
                'createtime' => time()
            ]);

            if ($data['award_type'] != 5) {
                if ($data['award_type'] == 0) {
                    $order['status'] = 0;
                    $name = $data['name'];
                } else {
                    $order['status'] = 1;
                    switch ($data['award_type']) {
                        case 1:
                            $model = new MoneyLog();
                            $key = 'money';
                            $type_name = "银币";
                            break;
                        case 2:
                            $model = new GoldLog();
                            $key = 'gold';
                            $type_name = "金币";
                            break;
                        case 3:
                            $model = new ScoreLog();
                            $key = 'score';
                            $type_name = "积分";
                            break;
                        case 4:
                            $model = new DiamondLog();
                            $key = 'diamond';
                            $type_name = "钻石";
                            break;
                    }
                    $before_aw = $user[$key];
                    $after_aw = function_exists('bcadd') ? bcadd($user[$key], $data['amount'], 2) : $user[$key] + $data['amount'];
                    if ($data['award_type'] == 3) {
                        $after = $after + $data['amount'];
                    } else {
                        $user->$key = $after_aw;
                    }
                    $model->insertGetId([
                        'user_id' => $user->id, $key => $data['amount'],
                        'before' => $before_aw, 'after' => $after_aw,
                        'memo' => "抽奖奖励", 'type' => 7,
                        'createtime' => time()
                    ]);
                    $name = "{$data['amount']}{$type_name}";
                }

                \app\common\model\scroll\Notify::log("获得 {$name}", $user->nickname, 2);

                $order_sn = get_sn($user->id);
                $order['user_id'] = $user->id;
                $order['order_sn'] = $order_sn;
                $order['name'] = $data['name'];
                $order['cover'] = $data['logo'];
                $order['score'] = $lottery_score;
                $order['type'] = 2;//抽奖
                Orders::create($order);
                $user->score = $after;
            }
            $user->save();
            Db::commit();
        } catch (Exception $exception) {
            Db::rollback();
            $this->error($exception->getMessage());
//            $this->success("", ['id' => 0, 'name' => '祝你好运', 'logo' => $this->request->domain() . 'assets/img/good/png', 'award_type' => 0]);
        }

        return $this->success("", $data);
    }

}